package com.hanyxx.io;

import java.io.*;

/**
 * 验证字节缓冲流的效率（文件大小为10M）
 * @author layman
 * @date 2021/3/9
 */
public class Demo08 {
    public static void main(String[] args) throws IOException {
        //testInputAndOut();
        testBufferInputAndOut();
    }

    // 测试普通字节流的效率
    private static void testInputAndOut() throws IOException {
        FileInputStream fis = new FileInputStream("D:\\葫芦小金刚.3gp");
        FileOutputStream fos = new FileOutputStream("D:\\葫芦小金刚_copy.3gp");

        long Start = System.currentTimeMillis();
        int len;
        while((len = fis.read())!=-1){
            fos.write(len);
        }

        long end = System.currentTimeMillis();
        // 普通字节流复制文件，耗时：58468毫秒
        System.out.println("普通字节流复制文件，耗时：" + (end - Start) + "毫秒");
    }
    //测试字节缓冲流的效率
    private static void testBufferInputAndOut() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\葫芦小金刚.3gp"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\葫芦小金刚_copy1.3gp"));
        long Start = System.currentTimeMillis();

        int len;
        byte[] bytes = new byte[1024];
        while((len =bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }
        long end = System.currentTimeMillis();
        //字节缓冲流复制文件，耗时：50毫秒
        System.out.println("字节缓冲流复制文件，耗时：" + (end - Start) + "毫秒");
    }
}
